var a = require("../../../@babel/runtime/helpers/interopRequireDefault").default, t = require("../../../config/Links"), e = a(require("../../../behaviors/aucMiniAuth")), o = a(require("../../../behaviors/auth.js")), n = a(require("../../../api/api_shareCard")), i = a(require("../../../utils/Tips")), s = a(require("@vant/weapp/dialog/dialog.js")), c = getApp(), l = c.eventBus, r = 0;

Component({
    behaviors: [ e.default, o.default ],
    data: {
        shareCardImageBaseUrl: t.imageBaseUrl.shareCard,
        shareId: "",
        windowWidth: "",
        windowHeight: "",
        pixelRatio: 2,
        defaultUserInfo: {
            nickName: "你的好友",
            avatarUrl: "https://txwk.10010.com/miniprogram_kcard/shareCard/headimg_default2.png"
        },
        headImg: "",
        qrcode: "",
        bgImg: t.imageBaseUrl.shareCard + "index_v3/qr_bg.png",
        bgImg_local: "",
        poster: "",
        picHasSaved: !1,
        showPoster: !1,
        showBtns: !1,
        actionEndFlag: !1,
        list: []
    },
    attached: function() {
        console.log("进入活动首页 attached，初始化全局参数"), c.globalData.loginCode = !1, c.globalData.hasAuth = !1, 
        c.globalData.aucMiniAuth.sid = "", c.globalData.actionEndFlag = !1, this.auth().then(function() {}), 
        this.getShareRecord();
    },
    methods: {
        showTimePassed: function() {
            i.default.toast("活动已过期");
        },
        auth: function() {
            var a = this;
            return new Promise(function(t, e) {
                wx.showLoading({
                    title: "加载中"
                });
                var o = a;
                n.default.getLogoList().then(function(a) {
                    o.setData({
                        showBtns: !0
                    }), c.globalData.aucPhone && o.setData({
                        phone: c.globalData.aucPhone
                    });
                    var e = a.code, n = a.result;
                    "0" === e && (n && n.actionEndFlag ? (o.setData({
                        actionEndFlag: !0
                    }), c.globalData.actionEndFlag = !0, wx.hideShareMenu()) : (o.setData({
                        actionEndFlag: !1
                    }), c.globalData.actionEndFlag = !1, wx.showShareMenu())), console.log("调用鉴权接口完成，展示按钮", c.globalData.aucMiniAuth, c.globalData.aucPhone, c.globalData.actionEndFlag), 
                    t();
                }).catch(function(a) {
                    o.setData({
                        showBtns: !1
                    }), wx.showModal({
                        content: "鉴权失败",
                        showCancel: !1
                    }), wx.hideLoading(), e();
                }).then(function() {
                    wx.hideLoading();
                });
            });
        },
        click_timeline: function() {
            var a = this;
            if (this.data.picHasSaved) return this.setData({
                showPoster: !0
            }), i.default.toast("您已保存过图片，可在本地相册中查看，分享到朋友圈"), !1;
            wx.showLoading({
                title: "分享海报生成中..."
            }), this.getHeadImg().then(function() {
                a.initCanvas();
            });
        },
        getHeadImg: function() {
            var a = this;
            return new Promise(function(t, e) {
                n.default.avatarUrl().then(function(e) {
                    console.log("接口获取用户头像成功", e);
                    var o = e.code, n = e.data;
                    "0" === o && n && n.nickname && n.avatarUrl ? (c.globalData.userInfo.nickName = n.nickname, 
                    c.globalData.userInfo.avatarUrl = n.avatarUrl, t()) : a.getHeadImg_fromWX();
                }).catch(function(t) {
                    console.log("接口获取用户头像失败", t), a.getHeadImg_fromWX();
                });
            });
        },
        getHeadImg_fromWX: function() {
            var a = this;
            return new Promise(function(t, e) {
                var o = a;
                wx.getSetting({
                    success: function(a) {
                        a.authSetting["scope.userInfo"] ? o.bindgetuserinfo() : (wx.hideLoading(), s.default.alert({
                            context: o,
                            confirmButtonOpenType: "getUserInfo",
                            bindgetuserinfo: "bindgetuserinfo",
                            confirmButtonText: "获取头像",
                            showCancel: !1,
                            richTextMessage: !0,
                            richText: [ {
                                name: "div",
                                children: [ {
                                    type: "text",
                                    text: "需要获取您的用户信息，请点击确认"
                                } ]
                            } ]
                        }));
                    },
                    complete: function() {}
                });
            });
        },
        bindgetuserinfo: function() {
            var a = this, t = this;
            wx.getUserInfo({
                success: function(a) {
                    console.log("微信获取用户头像成功", a), c.globalData.userInfo = a.userInfo;
                },
                fail: function(t) {
                    console.log("微信获取用户头像失败", t), c.globalData.userInfo = a.data.defaultUserInfo;
                },
                complete: function() {
                    t.initCanvas();
                }
            });
        },
        initCanvas: function() {
            var a = this;
            wx.showLoading({
                title: "分享海报生成中..."
            });
            var t = this;
            c.globalData.aucMiniAuth.sid ? t.getWindowWidth().then(function() {
                return t.getImgUrl();
            }).then(function(a) {
                return Promise.all([ t.getTempImage(a), t.getTempImage_bg(), t.getTempImage_headImg() ]);
            }).then(function() {
                return t.drawImage();
            }).then(function() {
                return a.setData({
                    showPoster: !0
                }), t.savePoster();
            }).catch(function() {
                wx.hideLoading();
            }) : i.default.toast("合成图片缺少sid,请重新获取");
        },
        getWindowWidth: function() {
            var a = this;
            return new Promise(function(t, e) {
                wx.getSystemInfo({
                    success: function(e) {
                        console.log("系统信息", e), a.setData({
                            windowWidth: e.windowWidth,
                            windowHeight: e.windowHeight,
                            pixelRatio: e.pixelRatio
                        }), t();
                    },
                    fail: function() {
                        i.default.toast("获取系统信息失败"), e();
                    }
                });
            });
        },
        getImgUrl: function() {
            return new Promise(function(a, t) {
                var e = "";
                c.globalData.aucMiniAuth.sid ? e = "?page=".concat(encodeURI("pages/shareCard_newUser/index"), "&sid=").concat(c.globalData.aucMiniAuth.sid, "&channel=").concat(c.globalData.aucChannel) : (i.default.toast("缺少分享信息sid"), 
                t());
                var o = c.globalData.baseUrl + "/actops-client/v0/invite/miniPageCode" + e;
                console.log("获取二维码图片网络地址", o), a(o);
            });
        },
        getTempImage: function(a) {
            var t = this;
            return a ? new Promise(function(e, o) {
                var n = t;
                wx.getImageInfo({
                    src: a,
                    success: function(a) {
                        console.log("获取二维码图片临时地址成功", a), a.path && a.width > 0 && a.height > 0 ? (n.setData({
                            qrcode: a.path
                        }), e(a.path)) : (wx.showModal({
                            content: "获取二维码图片临时地址失败，尺寸或者路径错误",
                            showCancel: !1
                        }), o());
                    },
                    fail: function(a) {
                        console.log("获取二维码图片临时路径失败", a), i.default.toast("获取二维码图片临时路径失败"), o(a);
                    }
                });
            }) : (wx.showModal({
                content: "未获得分享二维码，请刷新重试",
                showCancel: !1
            }), !1);
        },
        getTempImage_bg: function() {
            var a = this;
            return new Promise(function(t, e) {
                var o = a;
                console.log("背景图片网络地址", a.data.bgImg), wx.getImageInfo({
                    src: a.data.bgImg,
                    success: function(a) {
                        console.log("获取背景图片临时地址成功", a), a.path && a.width > 0 && a.height > 0 ? (o.setData({
                            bgImg_local: a.path
                        }), t()) : (wx.showModal({
                            content: "获取背景图片临时地址失败，尺寸或路径错误",
                            showCancel: !1
                        }), e());
                    },
                    fail: function(a) {
                        console.log("获取背景图片临时路径失败", a), i.default.toast("获取背景图片临时路径失败"), e(a);
                    }
                });
            });
        },
        getTempImage_headImg: function() {
            var a = this;
            return new Promise(function(t, e) {
                var o = a;
                console.log("头像网络地址", c.globalData.userInfo.avatarUrl), wx.getImageInfo({
                    src: c.globalData.userInfo.avatarUrl,
                    success: function(a) {
                        debugger;
                        console.log("获取头像临时地址成功", a), a.path && a.width > 0 && a.height > 0 ? (o.setData({
                            headImg: a.path
                        }), t()) : wx.getImageInfo({
                            src: o.data.defaultUserInfo.avatarUrl,
                            success: function(a) {
                                o.setData({
                                    headImg: a.path
                                }), t();
                            }
                        });
                    },
                    fail: function(a) {
                        console.log("获取头像临时地址失败", a), wx.getImageInfo({
                            src: o.data.defaultUserInfo.avatarUrl,
                            success: function(a) {
                                o.setData({
                                    headImg: a.path
                                }), t();
                            }
                        });
                    }
                });
            });
        },
        drawImage: function() {
            var a = this;
            return new Promise(function(t, e) {
                a.data.bgImg_local && a.data.qrcode || (wx.showModal({
                    content: "未获取到背景图或二维码，请重新进入页面",
                    showCancel: !1
                }), wx.hideLoading(), e()), console.log("drawImage");
                var o = a, n = wx.createCanvasContext("canvasPoster", o), i = o.data.windowWidth, s = 1.84 * o.data.windowWidth;
                n.drawImage(o.data.bgImg_local, 0, 0, i, s), n.save();
                var l = .38 * i, g = .5 * i - .5 * l, d = 1.09 * i;
                n.drawImage(o.data.qrcode, g, d, l, .95 * l), n.save();
                var u = .08 * i, h = .038 * i;
                console.log("top", h);
                var f = .13 * i, w = u, m = h, v = .5 * f, I = 2 * v, p = w + v, x = m + v;
                n.arc(p, x, v, 0, 2 * Math.PI), n.clip(), n.drawImage(a.data.headImg, w, m, I, I), 
                n.restore(), n.save(), n.strokeStyle = "#333";
                var b = i / 28;
                n.font = parseInt(b) + "px PingFang SC", n.fillText(c.globalData.userInfo.nickName, u + f + 10, h + .4 * f), 
                n.fillText("邀你一起领会员", u + f + 10, h + .85 * f), n.save(), n.draw(), setTimeout(function() {
                    wx.canvasToTempFilePath({
                        canvasId: "canvasPoster",
                        x: 0,
                        y: 0,
                        destWidth: 2 * i,
                        destHeight: 2 * s,
                        success: function(a) {
                            console.log("合成图片成功", a), o.setData({
                                poster: a.tempFilePath
                            }), t();
                        },
                        fail: function(a) {
                            o.setData({
                                poster: ""
                            }), (r += 1) < 3 ? o.drawImage() : wx.showModal({
                                title: "合成图片失败,请重新进入页面获取",
                                content: a.errMsg,
                                showCancel: !1
                            }), console.log("合成图片失败", a), e();
                        },
                        complete: function() {
                            wx.hideLoading();
                        }
                    }, o);
                }, 200);
            });
        },
        savePoster: function() {
            var a = this, t = this;
            this.data.poster ? this.data.picHasSaved || wx.saveImageToPhotosAlbum({
                filePath: this.data.poster,
                success: function(t) {
                    wx.showToast({
                        title: "分享海报已保存，快去分享给好友吧。",
                        icon: "none"
                    }), a.setData({
                        picHasSaved: !0
                    });
                },
                fail: function() {
                    wx.showModal({
                        content: "保存失败，请授权允许使用我的相册",
                        showCancel: !1,
                        success: function() {
                            wx.openSetting({
                                success: function(a) {
                                    a.authSetting["scope.writePhotosAlbum"] ? t.savePoster() : wx.showModal({
                                        content: "您没有打开保存相册功能，不能保存图片哦~点击右上角设置允许访问相册",
                                        showCancel: !1
                                    });
                                },
                                fail: function() {
                                    i.default.toast("打开设置失败");
                                }
                            });
                        }
                    });
                }
            }) : i.default.toast("无图片，请重新进入活动页面");
        },
        iSee: function() {
            this.setData({
                showPoster: !1
            });
        },
        closeDialog_openData: function() {},
        navigateToQrcode: function() {
            c.kta.clickStat("6aa062fff288457792f8ae4ce9c13df7"), wx.navigateTo({
                url: "/pages/shareCard_qrcode/index?adtag=" + c.globalData.aucChannel
            }), l.emit("gotoQrcode", {});
        },
        gotoMygifts: function() {
            c.kta.clickStat("6108d991d29a4e3e86d0e71259720abb"), wx.navigateTo({
                url: "/pages/shareCard_mygifts/index?adtag=" + c.globalData.aucChannel
            }), l.emit("gotoMygifts", {});
        },
        gotoRecord: function() {
            wx.navigateTo({
                url: "/pages/shareCard_record/index?adtag=" + c.globalData.aucChannel
            }), l.emit("gotoRecord", {});
        },
        gotoRules: function() {
            wx.navigateTo({
                url: "/pages/shareCard_rules/index?adtag=" + c.globalData.aucChannel
            });
        },
        getShareRecord: function() {
            var a = this;
            wx.showLoading({
                title: "加载中"
            });
            n.default.latest({
                num: 100
            }).then(function(t) {
                wx.hideLoading(), console.log("获取邀请成功记录成功", t);
                var e = t.code, o = t.result;
                t.msg;
                "0" === e && a.setData({
                    list: o
                });
            }).catch(function(a) {
                console.log("获取分享记录失败", a), wx.hideLoading();
            });
        }
    }
});